<?php



include_once("core/controller.php");

/**
 *
 * Login 
 * 
 * Class providing a login 
 *   
 * @copyright inovia
 * @package oolime_core   
 */   

class login extends Controller{


  public function isLegitimate(){
    if(!$this->request->containsDKeys(Array("user")) && (!$this->request->containsDKeys(Array("password")))) {
      return false;
    }
    if(!$this->request->containsDKeys(Array("user"))) {
      $this->response->addError("Please, enter your username","username");
      return false;
    }

    if(!$this->request->containsDKeys(Array("password")) ) {
      $this->response->addError("Please, enter your password","password");
      return false;
    }
    return true;
  }

  

  public function actions(){    
    $this->response->addDebug("Controller : user : ".$this->request->getValueD("user")." pass : ".md5($this->request->getValueD("password")));

    if(!($this->session->init($this->request->getValueD("user"),md5($this->request->getValueD("password"))))) {
      $this->response->addError("Invalid password or login.","login/password");
      $this->defaultActions();
    }else{
      $this->response->addDebug("You are now logged as :");
      $this->response->addDebug("Login : ".$this->user->field->user." - group : ".$this->user->field->group);
		  $Node = $this->response->doc->createElement("userlog");
			$Node->setAttribute("login",$this->request->getValueD("user"));
			$this->response->add($Node);
			$this->response->initSession();
    }
    
    if($this->user!=null && $this->user->isOk()){
      $this->user->changeMod('rw');
      $this->user->field->lastLogin = time();
      $node  = $this->response->doc->createElement("url");
      $node->appendChild($this->response->doc->createTextNode("0; url=".$this->request->get("url")));
      $this->response->add($node);
      $this->response->setXSLT("views/redirectLogin.xsl");
    }else{
      $this->defaultActions();
    }
  }

  

  public function defaultActions(){
    $node  = $this->response->doc->createElement("url");
    $node->appendChild($this->response->doc->createTextNode($_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']));
    $this->response->add($node);
    $this->response->setXSLT("views/login.xsl");
  }

}

?>
